class CustomerReviewsController < ApplicationController
  before_action :set_menu

  def index
    @submenu = :customer_review
    @title = '客户回访任务'
    @customer_reviews = CustomerReview.all
  end

  def new
  	@title = "客户回访任务/新增"
    @buttons.push :action => customer_reviews_path, :label => "返回"
    @customer_review = CustomerReview.new
    if params.has_key? :check 
      @car_num = params[:n1]+params[:n2]+params[:n3]
      @customer = Customer.find_by_car_num @car_num
      if @customer.nil?
        @infos.push "没有客户记录"
      end
    end
  end

  def create
    @customer_review = CustomerReview.new post_params
    if @customer_review.save
      redirect_to customer_reviews_path
    else
      @infos.push @customer_review.errors.full_messages
      render :action => 'new'
    end
  end

  def edit
    @title = "客户回访任务/修改"
    @buttons.push :action => customer_reviews_path, :label => "返回"
    @customer_review = CustomerReview.find params[:id]
  end

  def update
    @customer_review = CustomerReview.find params[:id]
    if @customer_review.update_attributes post_params
      redirect_to customer_reviews_path
    else
      render :action => 'edit'
    end
  end

  def destroy
    @customer_review = CustomerReview.find params[:id]
    @customer_review.destroy
    redirect_to customer_reviews_path
  end

  def record
    @title = "客户回访任务/记录"
    @buttons.push :action => customer_reviews_path, :label => "返回"
    @customer_review = CustomerReview.find params[:id]
    if @customer_review.save
      @customer_review.update(status: 1)
    end
  end


  def post_params
    params.require(:customer_review).permit(*%w(customer_id plan_review review_times task_type review_score contact_fail review_method actual_review is_success review_result review_advise user_id ))
  end

  private
  def set_menu
    @mainmenu = :customer
  end
end
